package com.ihome.android;

import java.util.Collection;
import java.util.List;
import java.util.Stack;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.content.Intent;

import com.ihome.android.util.log.LogUtil;

public class ActivityMgr {
	private static final String		TAG		= ActivityMgr.class.getSimpleName();
	private static Stack<Activity>	stack;
	private static ActivityMgr		instance;

	private ActivityMgr() {}

	public synchronized Activity add(Activity activity) {
		if (stack == null) {
			stack = new Stack<Activity>();
		}
		stack.add(activity);
		LogUtil.d(TAG, "activityStack size---->" + stack.size());
		return activity;
	}

	/**
	 * 单一实例
	 */
	public static ActivityMgr getInstance() {
		if (instance == null) {
			instance = new ActivityMgr();
		}
		return instance;
	}

	public boolean empty() {
		return stack.empty();
	}

	public synchronized Activity peek() {
		return stack.peek();
	}

	public synchronized Activity pop() {
		return stack.pop();
	}

	public synchronized Activity push(Activity activity) {
		if (stack == null) {
			stack = new Stack<Activity>();
		}
		if (contains(activity)) {
			LogUtil.d(TAG, "push contains(activity)" + activity.getClass().getName());
			remove(activity);
		}
		Activity pushActivity = stack.push(activity);
		LogUtil.d(TAG, "push size--->" + stack.size());
		return pushActivity;
	}

	public synchronized Activity remove(int i) {
		return stack.remove(i);
	}

	public synchronized boolean remove(Activity activity) {
		return stack.remove(activity);
	}

	public synchronized boolean removeAll(Collection<Activity> collection) {
		return stack.removeAll(collection);
	}

	public synchronized int search(Activity activity) {
		return stack.search(activity);
	}

	public synchronized int size() {
		return stack.size();
	}

	public synchronized boolean contains(Activity activity) {
		return stack.contains(activity);
	}

	public synchronized Activity get(int location) {
		return stack.get(location);
	}

	/**
	 * 判断当前Activity是否在最上面
	 */
	public boolean isTopActivity(Activity activity) {
		boolean isTop = false;
		if (activity != null) {
			if (activity == stack.peek()) {
				isTop = true;
			}
			else {
				isTop = false;
			}
		}
		LogUtil.v(ActivityMgr.this, " isTopActivity -- " + activity.toString() + " -- is" + isTop);
		return isTop;
	}

	/**
	 * 结束指定的Activity
	 */
	public void finishActivity(Activity activity) {
		if (activity != null) {
			stack.remove(activity);
			activity.finish();
			activity = null;
		}
	}

	/**
	 * 移除指定的Activity
	 */
	public void removeActivity(Activity activity) {
		if (activity != null) {
			stack.remove(activity);
			activity = null;
		}
	}

	/**
	 * 结束指定类名的Activity
	 */
	public void finishActivity(Class<?> cls) {
		for (Activity activity : stack) {
			if (activity.getClass().equals(cls)) {
				finishActivity(activity);
			}
		}
	}

	/**
	 * 结束所有Activity
	 */
	public void finishAllActivity() {
		for (int i = size() - 1; i >= 0; i--) {
			LogUtil.d(TAG, "  结束之前 size-->" + size());
			if (null != stack.get(i)) {
				LogUtil.d(TAG, " finish activityStack " + i + ":-->" + stack.get(i).getClass().getName());
				finishActivity(stack.get(i));
			}
			else {
				LogUtil.d(TAG, " finish activityStack  null " + i + ":-->" + stack.get(i).getClass().getName());
			}
		}
		LogUtil.d(TAG, "  结束之后 size-->" + size());
		stack.clear();
	}

	/**
	 * 退出应用程序
	 * 
	 * @param context
	 *            上下文
	 * @param isBackground
	 *            是否开开启后台运行
	 */
	public void AppExit(Context context, Boolean isBackground) {
		try {
			finishAllActivity();
			android.os.Process.killProcess(android.os.Process.myPid());
		} catch (Exception e) {

		} finally {
			// 注意，如果您有后台程序运行，请不要支持此句子
			if (!isBackground) {
				System.exit(0);
			}
		}
	}

	/**
	 * 退出应用(其他退出方法，在时间长之后或者是客户端崩溃后均无效)
	 */
	public static void getRunningActivityNum(Context context) {
		ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> runningTasks = am.getRunningTasks(30);
		for (int i = 0; i < runningTasks.size(); i++) {
			RunningTaskInfo runInfo = runningTasks.get(i);
			LogUtil.d(TAG, "runInfo:--->" + runInfo.baseActivity.getClassName());
		}
	}

	public static void exitAll(Context context) {
		Intent startMain = new Intent(Intent.ACTION_MAIN);
		startMain.addCategory(Intent.CATEGORY_HOME);
		startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(startMain);
		System.exit(0);// 退出程序
	}
}